<?php
	namespace Qzn\Model;
	
	class WallHandler {
		private $m_db = null;
		private $m_v = null;
		
		public function __construct(Database $db, Validate $v) {
			$this->m_db = $db;
			$this->m_v = $v;
		}
		
		/**
		 * @param LoginView
		 * @return array(array($messageID, $userID, $userName, $date, $message))
		 */
		public function GetMessages(\Qzn\View\LoginView $lw) {
			$messages = array();
			$fl = DatabaseQuery::SelectFriendList($lw->GetUserPk(), $this->m_db);
			$messages[] = DatabaseQuery::GetMessages($lw->GetUserPk(), $this->m_db);
			foreach($fl as $value) {
				if ($value[1] === $lw->GetUserPk()) {
					$messages[] = DatabaseQuery::GetMessages($value[2], $this->m_db);
				} else {
					$messages[] = DatabaseQuery::GetMessages($value[1], $this->m_db);
				}
			}
			return $messages;
		}
		
		/**
		 * @param MessageId
		 * @return array($messageID, $userID, $userName, $date, $message)
		 */
		public function GetMessage($messageId, \Qzn\Model\WallHandler $wh) {
			$message = DatabaseQuery::GetMessage($messageId, $this->m_db);
			$messageLikesRet = array();
			$likes = 0;
			$messageLikes = $wh->GetMessageLikes($message[0][0]);
			
			foreach($messageLikes as $a) {
				$likes += $a[1] === 0 ? -1 : 1;
				$messageLikesRet[$a[0]] = $a[1];
			}
			
			$message = new Message($message[0][0], $message[0][1], $message[0][2], $message[0][3], $message[0][4], $likes, $messageLikesRet);
			
			return $message;
		}
		
		
		/**
		 * @param int $messagePk
		 * @return array($commentID, $userID, $userName, $date, $comment)
		 */
		public function GetComments($messagePk) {
			return DatabaseQuery::GetComments($messagePk, $this->m_db);
		}
		
		/**
		 * @param int $messagePk
		 * @return array(int $userPk, bit $like)
		 */
		public function GetMessageLikes($messagePk) {
			return DatabaseQuery::GetMessageLikes($messagePk, $this->m_db);
		}
		
		/**
		 * @param int $messagePk
		 * @return array(int $userPk, bit $like)
		 */
		public function GetCommentLikes($commentPk) {
			return DatabaseQuery::GetCommentLikes($commentPk, $this->m_db);
		}
		
		/**
		 * @param LoginView
		 * @param WallView
		 */
		public function SendPost(\Qzn\View\WallView $ww, \Qzn\View\LoginView $lw) {
			DatabaseQuery::InsertMessage($lw->GetUserPk(), $this->m_v->ValidateString($ww->GetMessage(), false), $this->m_db);
		}
		
		/**
		 * @param LoginView
		 * @param WallView
		 */
		public function SendComment(\Qzn\View\WallView $ww, \Qzn\View\LoginView $lw) {
			if (strlen($ww->GetComment()) != 0) {
				DatabaseQuery::InsertComment($lw->GetUserPk(), $ww->GetMessagePk(), $this->m_v->ValidateString($ww->GetComment(), false), $this->m_db);
			}
		}
		
		/**
		 * @param LoginView
		 * @param WallView
		 */
		public function SendCommentLike(\Qzn\View\WallView $ww, \Qzn\View\LoginView $lw) {
			$like = $this->GetCommentLikes($ww->GetLikeCommentPk());
			if ($like == null) {
				DatabaseQuery::InsertCommentLike($lw->GetUserPk(), $ww->GetLikeCommentPk(), $this->m_db);
			} else {
				$procced = false;
				$found = false;
				foreach($like as $a) {
					if ($a[0] === $lw->GetUserPk()) {
						$found = true;
						if ($a[1] === 0) {
							$procced = true;
						}
						break;
					}
				}
				if ($procced || !$found) {
					DatabaseQuery::DeleteCommentLike($lw->GetUserPk(), $ww->GetLikeCommentPk(), $this->m_db);
					DatabaseQuery::InsertCommentLike($lw->GetUserPk(), $ww->GetLikeCommentPk(), $this->m_db);
				}
			}
		}
		
		/**
		 * @param LoginView
		 * @param WallView
		 */
		public function SendCommentDislike(\Qzn\View\WallView $ww, \Qzn\View\LoginView $lw) {
			$like = $this->GetCommentlikes($ww->GetLikeCommentPk());
			if ($like == null) {
				DatabaseQuery::InsertCommentDislike($lw->GetUserPk(), $ww->GetLikeCommentPk(), $this->m_db);
			} else {
				$procced = false;
				$found = false;
				foreach($like as $a) {
					if ($a[0] === $lw->GetUserPk()) {
						$found = true;
						if ($a[1] === 1) {
							$procced = true;
						}
						break;
					}
				}
				if ($procced || !$found) {
					DatabaseQuery::DeleteCommentLike($lw->GetUserPk(), $ww->GetLikeCommentPk(), $this->m_db);
					DatabaseQuery::InsertCommentDislike($lw->GetUserPk(), $ww->GetLikeCommentPk(), $this->m_db);
				}
			}
		}
		
		/**
		 * @param LoginView
		 * @param WallView
		 */
		public function SendMessageLike(\Qzn\View\WallView $ww, \Qzn\View\LoginView $lw) {
			$like = $this->GetMessageLikes($ww->GetLikeMessagePk());
			if ($like == null) {
				DatabaseQuery::InsertMessageLike($lw->GetUserPk(), $ww->GetLikeMessagePk(), $this->m_db);
			} else {
				$procced = false;
				$found = false;
				foreach($like as $a) {
					if ($a[0] === $lw->GetUserPk()) {
						$found = true;
						if ($a[1] === 0) {
							$procced = true;
						}
						break;
					}
				}
				if ($procced || !$found) {
					DatabaseQuery::DeleteMessageLike($lw->GetUserPk(), $ww->GetLikeMessagePk(), $this->m_db);
					DatabaseQuery::InsertMessageLike($lw->GetUserPk(), $ww->GetLikeMessagePk(), $this->m_db);
				}
			}
		}
		
		/**
		 * @param LoginView
		 * @param WallView
		 */
		public function SendMessageDislike(\Qzn\View\WallView $ww, \Qzn\View\LoginView $lw) {
			$like = $this->GetMessageLikes($ww->GetLikeMessagePk());
			if ($like == null) {
				DatabaseQuery::InsertMessageDislike($lw->GetUserPk(), $ww->GetLikeMessagePk(), $this->m_db);
			} else {
				$procced = false;
				$found = false;
				foreach($like as $a) {
					if ($a[0] === $lw->GetUserPk()) {
						$found = true;
						if ($a[1] === 1) {
							$procced = true;
						}
						break;
					}
				}
				if ($procced || !$found) {
					DatabaseQuery::DeleteMessageLike($lw->GetUserPk(), $ww->GetLikeMessagePk(), $this->m_db);
					DatabaseQuery::InsertMessageDislike($lw->GetUserPk(), $ww->GetLikeMessagePk(), $this->m_db);
				}
			}
		}
	}
?>